
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author GON
 */
public class SendFile {

    private Socket connection;
    private ObjectOutputStream outStream;

    public SendFile(Socket connection) {
        this.connection = connection;
        try {
            outStream = new ObjectOutputStream(
                    connection.getOutputStream());
            outStream.flush();

        } catch (IOException e) {
            System.out.println("Output stream Error!");
        }
    }

    public void streamFile(File file) throws IOException{
        long fileSize = file.length();
        long completed  = 0;
        int step = 150000;

        // cretes the file stream
        FileInputStream fileStream = new FileInputStream(file);

        // sendign a message before streaming the file
        outStream.writeObject ("SENDING FILE|"
                + file.getName()+
                "|"+ fileSize);

        byte[] buffer = new byte[step];
        while(completed <= fileSize){
            fileStream.read(buffer);
            outStream.write(buffer);
            completed += step;
        }
        outStream.writeObject("SEND_COMPLETE");
        fileStream.close();
    }
}
